/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};

/**
 * 计算table的 selectedRows
 * @param {*} preKeys 上一个操作 返回的 selectedKeys
 * @param {*} preRows 上一个操作 返回的 selectedRows
 * @param {*} currentKeys 本次操作 返回的 selectedKeys
 * @param {*} currentRows 本次操作 返回的 selectedRows
 * @param {*} keyIndex eg: id
 */
import difference from 'lodash/difference';
export const computeSelectedRows = (preKeys, preRows, currentKeys, currentRows, keyIndex = 'id') => {
    if(currentKeys.length === 0)
        return [];
    // 勾选
    if(currentKeys.length > preKeys.length) {
        const newKeys = difference(currentKeys, preKeys);
        const newRows = [];
        newKeys.forEach(key => {
            const row = currentRows.find(r => r[keyIndex] === key);
            if(row)
                newRows.push(row);
        });
        return [...preRows, ...newRows];
    } else if(currentKeys.length < preKeys.length) { // 取消勾选
        const deleteKeys = difference(preKeys, currentKeys);
        const newRows = preRows.filter(d => !deleteKeys.includes(d[keyIndex]));
        return newRows;
    }
    return preRows;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);
import {formatMessage} from './intl';
export const validateAdd = data => {
    const {brandId, dealerId, direction, sourceType, reason, laborFee = 0, materialFee = 0, mgmtFee = 0, outFee = 0} = data;
    const fee = laborFee + materialFee + mgmtFee + outFee;
    const msgs = [];
    if(!brandId)
        msgs.push(
            formatMessage({
                id: 'utils.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!dealerId)
        msgs.push(
            formatMessage({
                id: 'utils.dealer',
                defaultMessage: '总代'
            })
        );
    if(!direction)
        msgs.push(
            formatMessage({
                id: 'utils.direction',
                defaultMessage: '扣补款方向'
            })
        );
    if(!sourceType)
        msgs.push(
            formatMessage({
                id: 'utils.sourceType',
                defaultMessage: '源单据类型'
            })
        );
    if(!reason)
        msgs.push(
            formatMessage({
                id: 'utils.reason',
                defaultMessage: '扣补款原因'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};
